<template>
  <el-form ref="funcForm" :model="func" :rules="rules" label-width="100px" class="demo-ruleForm">
    <el-form-item label="功能名称" prop="title">
      <el-input v-model="func.title" />
    </el-form-item>
    <el-form-item label="Controller名称" prop="controllerName">
      <el-input v-model="func.controllerName" />
    </el-form-item>
    <el-form-item label="Action名称" prop="actionName">
      <el-input v-model="func.actionName" />
    </el-form-item>
    <el-form-item label="请求方式" prop="method">
      <el-select v-model="func.method" placeholder="请选择提交方式">
        <el-option label="Get" value="Get" />
        <el-option label="Post" value="Post" />
        <el-option label="Put" value="Put" />
        <el-option label="Delete" value="Delete" />
      </el-select>
    </el-form-item>

    <el-form-item label="图标" prop="icon">
      <el-input v-model="func.icon" />
    </el-form-item>
    <el-form-item label="前端Url" prop="url">
      <el-input v-model="func.url" />
    </el-form-item>
    <el-form-item label="是否菜单" prop="isMenu">
      <el-switch v-model="func.isMenu" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('funcForm')">立即创建</el-button>
      <el-button @click="resetForm('funcForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import request from '@/utils/request'
export default {
  props: ['funcdata'],
  data() {
    return {
      func: {
        title: '',
        actionName: '',
        controllerName: '',
        method: '',
        url: '',
        parentId: 0,
        icon: '',
        isMenu: false
      },
      rules: {
        title: [
          { required: true, message: '请输入功能名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          this.func.parentId = this.funcdata.id || 0
          await request({ url: '/functions', method: 'post', data: this.func })
          this.$emit('closeWin', true)
          this.resetForm(formName)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
